package com.project.fiveCerealsSavoury.service.impl;

import java.util.ArrayList;
import java.util.List;
import org.junit.Ignore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.project.fiveCerealsSavoury.dao.GoodsDao;
import com.project.fiveCerealsSavoury.domain.Goods;
import com.project.fiveCerealsSavoury.exception.BizException;
import com.project.fiveCerealsSavoury.service.GoodsService;
import com.project.fiveCerealsSavoury.transfer.Page;
import com.project.fiveCerealsSavoury.transfer.UserPage;
@Service
@Transactional
public class GoodsServiceImpl implements GoodsService {
	@Autowired
	private GoodsDao goodsDao;
	@Override
	public Goods add(Goods goods) throws BizException {
		if(goods.getCount() == null)throw new BizException("货物数量不能为空");
		if(goods.getGoodName() == null)throw new BizException("货物名称不能为空");
		if(goods.getNowPrice() == null)throw new BizException("货物现价不能为空");
		if(goods.getPrice() == null)throw new BizException("货物价格不能为空");
		if(goods.getWeight() == null)throw new BizException("货物重量不能为空");
		if(goods.getUrl() == null)throw new BizException("货物图片不能为空");
		goodsDao.add(goods);
		return goods;
	}

	@Override
	public boolean amend(Integer id, String newGoodName, Double newPrice, Double newNowPrice, Integer newCount,
			Double newWeight)throws BizException {
		Goods goods = goodsDao.query(id);
		if(goods != null){
			if(newGoodName == null)throw new BizException("货物新名称不能为空");
			if(newPrice == null)throw new BizException("货物新原价不能为空");
			if(newNowPrice == null)throw new BizException("货物新现价不能为空");
			if(newCount == null)throw new BizException("货物新数量不能为空");
			if(newWeight == null)throw new BizException("货物新重量不能为空");
			goods.setGoodName(newGoodName);
			goods.setPrice(newPrice);
			goods.setNowPrice(newNowPrice);
			goods.setCount(newCount);
			goods.setWeight(newWeight);
			goodsDao.amend(id, newGoodName, newPrice, newNowPrice, newCount, newWeight);
			return true;
		}
		return false;
	}

	@Override
	public boolean remove(Integer id)throws BizException{
		if(id == null)throw new BizException("该商品不存在");
		Goods goods = goodsDao.query(id);
		if(goods != null){
			goodsDao.remove(id);
			return true;
		};
		return false;
	}

	@Override
	public List<Goods> queryAll(String goodName)throws BizException  {
		if(goodName == null)throw new BizException("货物名称不能为空");
		return goodsDao.queryAll(goodName);
	}

	@Override
	public Page list(UserPage userPage) {
		List<Goods> goods = new ArrayList<>();
		Page jsonPage = new Page();
		List<Goods> list = goodsDao.querys();
		int counts = goodsDao.count();//总商品数
		int total = counts/userPage.getPageSize()+(counts % userPage.getPageSize() > 0 ? 1:0);//总商品数除以当前页数的数量加总商品数除以当前页数的余数等于有总共多少页
		if(userPage.getPageIndex() > total){
			userPage.setPageIndex(total);
		}else if(userPage.getPageIndex() < 1){
			userPage.setPageIndex(1);
		}
		jsonPage.setPageIndex(userPage.getPageIndex());
		//当前开始下标
		int beginIndex = (userPage.getPageIndex() -1)*userPage.getPageSize();
		//结束下标
		int endIndex = beginIndex + userPage.getPageSize();
		if(endIndex > counts){
			endIndex = counts;
		}
		int i=0;
		for (Goods goods2 : list) {
			if(i >= beginIndex && i < endIndex){
				goods.add(goods2);
			}
			i++;
		}
		jsonPage.setTotal(total);
		jsonPage.setList(goods);
		return jsonPage;
	}

	@Override
	public Goods query(Integer id)throws BizException {
		if(id == null)throw new BizException("查询商品失败");
		return goodsDao.query(id);
	}
	/**
	 * 减库存
	 */
	@Override
	public int stocks(String goodName,Integer count) throws BizException {
		if(goodName == null)throw new BizException("商品名不能为空");
		if(count == null)throw new BizException("数量不能为空");
		return goodsDao.stocks(goodName,count);
	}

	@Override
	public List<Goods> que() {
		return goodsDao.que();
	}

	@Override
	public List<Goods> queryss(String goodName) throws BizException {
		if(goodName == null)throw new BizException("商品名不能为空");
		return goodsDao.queryss(goodName);
	}
}
